home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / Developer University / DUProjects / Graphics / Sources / Frame.cpp < prev    next >
Encoding:
Text File  |  1996-04-05  |  11.2 KB  |  386 lines  |  [TEXT/CWIE]

  1. //    Copyright © 1995 Apple Computer, Inc. All rights reserved.
  2. //    Release Version:    $ 1.0 d11 $
  3.  
  4. //=======================================================================
  5. #ifndef FRAME_H
  6. #include "Frame.h"
  7. #endif
  8.  
  9. #ifndef PART_H
  10. #include "Part.h"
  11. #endif
  12.  
  13. // ----- Framework Layer -----
  14. #ifndef FWUTIL_H
  15. #include "FWUtil.h"                // FW_CFacetContext, FW_Beep()
  16. #endif
  17.  
  18. #ifndef FWCONTXT_H
  19. #include "FWContxt.h"            // FW_CViewContext
  20. #endif
  21.  
  22. #ifndef FWPRHDLR_H
  23. #include "FWPrHdlr.h"            // FW_CPrintHandler
  24. #endif
  25.  
  26. // ----- OS Layer -----
  27. #ifndef FWMENU_H
  28. #include "FWMenu.h"                // FW_CMenuBar, etc.
  29. #endif
  30.  
  31. #ifndef FWEVENT_H
  32. #include "FWEvent.h"            // FW_CMenuEvent, FW_CMouseEvent
  33. #endif
  34.  
  35. #ifndef FWALERT_H
  36. #include "FWAlert.h"            // FW_CAlert
  37. #endif
  38.  
  39. #ifndef FWEVEDEF_H
  40. #include "FWEveDef.h"            // FW_BACKSPACE, etc.
  41. #endif
  42.  
  43. #ifndef FWCFMRES_H
  44. #include "FWCFMRes.h"            // FW_CSharedLibraryResourceFile
  45. #endif
  46.  
  47. #ifndef FWODGEOM_H
  48. #include "FWODGeom.h"            // FW_NewODShape
  49. #endif
  50.  
  51. #ifndef SLMIXCOS_H
  52. #include "SLMixOS.h"            // FW_Beep
  53. #endif
  54.  
  55. // ----- Graphic Includes -----
  56. #ifndef FWRECT_H
  57. #include <FWRect.h>                // FW_CRect
  58. #endif
  59.  
  60. #ifndef FWRECSHP_H
  61. #include "FWRecShp.h"            // FW_CRectShape
  62. #endif
  63.  
  64. #ifndef FWPOLY_H
  65. #include "FWPoly.h"                // FW_PPolygon
  66. #endif
  67.  
  68. #ifndef FWPOLYSH_H
  69. #include "FWPolySh.h"            // FW_CPolygonShape
  70. #endif
  71.  
  72. #ifndef FWOVLSHP_H
  73. #include "FWOvlShp.h"            // FW_COvalShape
  74. #endif
  75.  
  76. #ifndef FWPICSHP_H
  77. #include "FWPicShp.h"            // FW_PPicture, FW_CPictureShape
  78. #endif
  79.  
  80. #ifndef FWTXTSHP_H
  81. #include "FWTxtShp.h"            // FW_CTextShape
  82. #endif
  83.  
  84. #ifndef FWLINSHP_H
  85. #include "FWLinShp.h"            // FW_CLineShape
  86. #endif
  87.  
  88. #ifndef FWRGNSHP_H
  89. #include "FWRgnShp.h"            // FW_CRegionShape
  90. #endif
  91.  
  92. //========================================================================================
  93. #ifdef FW_BUILD_MAC
  94. #pragma segment Graphics
  95. #endif
  96.  
  97. //========================================================================================
  98. FW_DEFINE_AUTO(CGraphicsFrame)
  99.  
  100. //========================================================================================
  101. CGraphicsFrame::CGraphicsFrame(Environment* ev, ODFrame* odFrame, 
  102.                                     FW_CPresentation* presentation, CGraphicsPart* part)
  103.   : FW_CFrame(ev, odFrame, presentation, part),
  104.       fLargePictureShape(NULL),
  105.       fLineShape(NULL),
  106.       fRegionShape(NULL),
  107.       fPolygonShape(NULL)
  108. {
  109.     fLargePictureShape = MyCreatePictureShape(ev);
  110.     fLineShape = MyCreateLineShape(ev);
  111.     fRegionShape = MyCreateRegionShape(ev);
  112.     fPolygonShape = MyCreatePolygonShape(ev);
  113.     FW_END_CONSTRUCTOR
  114. }
  115.  
  116. //----------------------------------------------------------------------------------------
  117. CGraphicsFrame::~CGraphicsFrame()
  118. {
  119.     FW_START_DESTRUCTOR
  120.     delete fLargePictureShape;
  121.     delete fLineShape;
  122.     delete fRegionShape;
  123.     delete fPolygonShape;
  124. }
  125.  
  126. //------------------------------------------------------------------------------
  127. FW_CPictureShape*
  128. CGraphicsFrame::MyCreatePictureShape(Environment* ev)
  129. {
  130.     FW_CSharedLibraryResourceFile resFile(ev);    // resource from our shared library
  131.  
  132.     // draw large picture - use natural picture size
  133.     const short kMacPictID = 2001;
  134.     const short kCatPictID = 2002;
  135.     const short kODFIconPictID = 2003;
  136.     FW_CPicture picture(resFile, kMacPictID);
  137.     picture.GetPictBounds(fLargePictRect);
  138.     return FW_NEW(FW_CPictureShape, (picture, fLargePictRect) );
  139. }
  140.  
  141. //------------------------------------------------------------------------------
  142. FW_CLineShape*
  143. CGraphicsFrame::MyCreateLineShape(Environment* ev)
  144. {
  145.     FW_CLineShape* shp = FW_NEW(FW_CLineShape,
  146.                         (FW_CPoint(FW_IntToFixed(200), FW_IntToFixed(70)),
  147.                          FW_CPoint(FW_IntToFixed(300), FW_IntToFixed(70)) ) );
  148.     shp->GetStyle().SetPenSize(FW_IntToFixed(5));
  149.     shp->GetInk().SetForeColor(FW_kRGBRed);
  150.     return shp;
  151. }
  152.  
  153. //------------------------------------------------------------------------------
  154. FW_CRegionShape*
  155. CGraphicsFrame::MyCreateRegionShape(Environment* ev)
  156. {
  157.     const FW_CPoint kSize( FW_IntToFixed(25), FW_IntToFixed(25) );
  158.     const FW_CPoint kOffset( FW_IntToFixed(32), FW_IntToFixed(0) );
  159.     const FW_Fixed kInset = FW_IntToFixed(4);
  160.  
  161.     FW_CRect fwR1(FW_kZeroPoint, kSize);
  162.     FW_CAcquiredODShape odR1 = FW_NewODShape (ev, fwR1);
  163.     
  164.     FW_CRect fwR2(fwR1);
  165.     fwR2.Offset(kOffset);
  166.     fwR2.Inset(kInset);
  167.     ODShape* odR2 = FW_NewODShape (ev, fwR2);
  168.  
  169.     odR2->Union(ev, odR1);
  170.     FW_CRegionShape* shp = FW_NEW(FW_CRegionShape, (odR2, FW_kFill) );
  171.     shp->GetInk().SetForeColor(FW_kRGBPurple);
  172.     shp->MoveShape(FW_IntToFixed(40), FW_IntToFixed(300));    // x, y
  173.     return shp;
  174. }
  175.  
  176. //------------------------------------------------------------------------------
  177. FW_CPolygonShape*
  178. CGraphicsFrame::MyCreatePolygonShape(Environment* ev)
  179. {
  180.     FW_CPoint* points = new FW_CPoint[8];
  181.     points[0] = FW_CPoint(FW_IntToFixed(230), FW_IntToFixed(330));
  182.     points[1] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(330));
  183.     points[2] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(320));
  184.     points[3] = FW_CPoint(FW_IntToFixed(290), FW_IntToFixed(340));
  185.     points[4] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(360));
  186.     points[5] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(350));
  187.     points[6] = FW_CPoint(FW_IntToFixed(230), FW_IntToFixed(350));
  188.     points[7] = FW_CPoint(FW_IntToFixed(230), FW_IntToFixed(330));
  189.     FW_CPolygon poly(8, points);
  190.     delete [] points;
  191.     FW_Boolean kAutoCloseFrame    = true;             
  192.     FW_CPolygonShape* shape = FW_NEW(FW_CPolygonShape, (poly, FW_kFill, kAutoCloseFrame));
  193.     return shape;
  194. }
  195.  
  196. //----------------------------------------------------------------------------------------
  197. void 
  198. CGraphicsFrame::FacetAdded(Environment* ev, ODFacet* facet, unsigned short facetCount)
  199. {        
  200.     FW_CFrame::FacetAdded(ev, facet, facetCount);
  201.     if ( this->IsRoot(ev) ) {
  202.         FW_CPoint interiorSize = fLargePictRect.BotRight();
  203.         this->GetWindow(ev)->SetWindowSize(ev, interiorSize);
  204.     }
  205. }
  206.  
  207. //----------------------------------------------------------------------------------------
  208. void 
  209. CGraphicsFrame::Draw(Environment *ev, ODFacet* odFacet, ODShape* invalidShape)    // Override
  210. {
  211.     FW_CViewContext context(ev, this, odFacet, invalidShape);
  212.     this->MyDrawBackground(ev, context);
  213.     this->MyDrawPictures(ev, context);
  214.     this->MyDrawShapes(ev, context);
  215.     this->MyDrawLine(ev, context);
  216.     this->MyDrawText(ev, context);
  217.     this->MyDrawPolygon(ev, context);
  218. }
  219.  
  220. //------------------------------------------------------------------------------
  221. void 
  222. CGraphicsFrame::MyDrawBackground(Environment* ev, FW_CGraphicContext& gc)
  223. {
  224.     FW_CRect box = this->GetBounds(ev);    
  225.     FW_CRectShape::RenderRect(gc, box, FW_kFill, FW_kRGBLightGray);
  226. }
  227.  
  228. //------------------------------------------------------------------------------
  229. void 
  230. CGraphicsFrame::MyDrawShapes(Environment* ev, FW_CGraphicContext& gc)
  231. {
  232.     FW_CRect boxRect(FW_IntToFixed(25), FW_IntToFixed (25), 
  233.                         FW_IntToFixed(100), FW_IntToFixed(100));
  234.     
  235.     // draw filled black box
  236.     FW_CRectShape     boxShape(boxRect, FW_kFill);
  237.     boxShape.Render(gc);
  238.  
  239.     // draw filled green box
  240.     boxShape.MoveShape(FW_IntToFixed(25), FW_IntToFixed(25));
  241.     boxShape.GetInk().SetForeColor(FW_kRGBGreen);
  242.     boxShape.Render(gc);
  243.     
  244.     // draw framed black box
  245.     boxRect.Offset(FW_IntToFixed(50), FW_IntToFixed(50));
  246.     FW_CRectShape::RenderRect(gc, boxRect, FW_kFrame);
  247.     
  248.     // draw oval inscribed in box; special pattern, transfer mode
  249.     boxRect.Offset(FW_IntToFixed(25), FW_IntToFixed(25));
  250.     const FW_CColor kForeColor(FW_kRGBRed);
  251.     const FW_CColor kBackColor(FW_kRGBBlue);
  252.     FW_CInk ink(kForeColor, kBackColor, FW_kXOr);
  253.     const FW_Fixed kPenSize = FW_IntToFixed(4);
  254.     const FW_CPattern kPattern = FW_kDiagCrossPat;
  255.     FW_CStyle style(kPenSize, kPattern);
  256.     FW_COvalShape::RenderOval(gc, boxRect, FW_kFill, ink, style);
  257.  
  258.     // draw oval frame
  259.     FW_COvalShape::RenderOval(gc, boxRect, FW_kFrame);
  260.     
  261.     // draw arrow
  262.     fRegionShape->Render(gc);
  263. }
  264.  
  265. //------------------------------------------------------------------------------
  266. void 
  267. CGraphicsFrame::MyDrawText(Environment* ev, FW_CGraphicContext& gc)
  268. {
  269.     FW_CString32 string("ODF");
  270.     FW_CPoint position( FW_IntToFixed(200), FW_IntToFixed(50) );
  271.  
  272.     // draw 12 point ODF
  273.     FW_CTextShape stringShape(string, position.x, position.y);
  274.     stringShape.Render(gc);
  275.  
  276.     // draw fancy ODF
  277.     const FW_Fixed kFontSize = FW_IntToFixed(48);
  278.     FW_CTextShape::RenderText(gc, 
  279.                             string, 
  280.                             position,
  281.                               FW_CFont(FW_kPalatino, FW_kItalic, kFontSize), 
  282.                               FW_kTextAlignLeft | FW_kTextAlignBottom,
  283.                               FW_CInk(FW_kRGBBlue, FW_kRGBWhite, FW_kOr));
  284. }
  285.  
  286. //------------------------------------------------------------------------------
  287. void 
  288. CGraphicsFrame::MyDrawPictures(Environment* ev, FW_CGraphicContext& gc)
  289. {
  290.     FW_CSharedLibraryResourceFile resFile(ev);    // resource from our shared library
  291.  
  292.     fLargePictureShape->Render(gc);
  293.     
  294.     // draw DevU logo - I choose size
  295.     const short kODFPictID = 2000;
  296.     const short kCatPictID = 2002;
  297.     const short kODFIconPictID = 2003;
  298.      FW_CPictureShape::RenderPicture(gc,
  299.                                     FW_CPicture(resFile, kODFPictID),
  300.                                     FW_CRect(FW_IntToFixed(25), FW_IntToFixed(175), FW_IntToFixed(125), FW_IntToFixed(275))
  301.                                     );
  302. }
  303.  
  304. //------------------------------------------------------------------------------
  305. void 
  306. CGraphicsFrame::MyDrawLine(Environment* ev, FW_CGraphicContext& gc)
  307. {
  308.     fLineShape->Render(gc);
  309. }
  310.  
  311. //------------------------------------------------------------------------------
  312. void 
  313. CGraphicsFrame::MyDrawPolygon(Environment* ev, FW_CGraphicContext& gc)
  314. {
  315.     fPolygonShape->Render(gc);
  316. }
  317.  
  318. //----------------------------------------------------------------------------------------
  319. FW_Boolean 
  320. CGraphicsFrame::DoMouseDown(Environment* ev, const FW_CMouseEvent& theMouseEvent)
  321. {    
  322.     FW_Boolean eventHandled = false;
  323.     FW_CPoint where = theMouseEvent.GetMousePosition(ev, FW_CMouseEvent::kFrame);
  324.     this->GetContentView(ev)->FrameToViewContent(ev, where);
  325.     ODFacet* theFacet = theMouseEvent.GetFacet(ev);
  326.     FW_CViewContext fc(ev, this, theFacet, NULL);                // invalidShape = NULL
  327.     const FW_Fixed kTolerance = FW_IntToFixed(2);
  328.     if ( fLineShape->HitTest(fc, where, kTolerance) ) {    
  329.         FW_Beep();
  330.         eventHandled = true;
  331.         this->MyInvalidateLine(ev);
  332.         fLineShape->MoveShape(FW_IntToFixed(0), FW_IntToFixed(20));        // move down
  333.         this->MyInvalidateLine(ev);        
  334.         }    // if
  335.  
  336.     if ( fRegionShape->HitTest(fc, where, kTolerance) ) {
  337.         FW_Beep();
  338.         eventHandled = true;
  339.         this->Invalidate(ev, fRegionShape->GetODShape() );
  340.         }    // if
  341.  
  342.     if ( fPolygonShape->HitTest(fc, where, kTolerance) ) {
  343.         FW_Beep();
  344.         eventHandled = true;
  345.         }    // if
  346.  
  347.     return eventHandled;
  348. }
  349.  
  350. //------------------------------------------------------------------------------
  351. void 
  352. CGraphicsFrame::MyInvalidateLine(Environment* ev)
  353. {
  354.     FW_CPoint start;
  355.     FW_CPoint end;
  356.     fLineShape->GetGeometry(start, end);
  357.     FW_CRect lineRect(start.x, start.y, end.x, end.y);
  358.     lineRect.Sort();
  359.     FW_Fixed halfPen = fLineShape->GetPenSize() / FW_IntToFixed(2);
  360.     lineRect.Inset(-halfPen, -halfPen);        // expand for pen size
  361.     this->Invalidate(ev, lineRect);            // force update
  362. }
  363.  
  364. //------------------------------------------------------------------------------
  365. FW_CPrintHandler* 
  366. CGraphicsFrame::NewPrintHandler(Environment* ev)
  367. {
  368.     FW_CPart* part = GetPart(ev);
  369.     return new FW_CPrintHandler(part, this);
  370. }
  371.  
  372. //------------------------------------------------------------------------------
  373. FW_Boolean 
  374. CGraphicsFrame::IsCurrentlyPrintable(Environment* ev) const
  375. {
  376.     return TRUE;
  377. }
  378.  
  379. //------------------------------------------------------------------------------
  380. void 
  381. CGraphicsFrame::GetPrintContentExtent(Environment *ev, FW_CPoint& extent) const
  382. {
  383.     extent = fLargePictRect.Size();
  384.     FW_ASSERT(extent.x != FW_kFixed0);
  385.     FW_ASSERT(extent.y != FW_kFixed0);
  386. }